===============================================
DEBUG GENERATE VOUCHER - TROUBLESHOOTING
===============================================

MASALAH: Generate voucher tidak berhasil
=========================================

KEMUNGKINAN PENYEBAB:
=====================

1. ❌ Session MikroTik tidak ditemukan
   → Agent panel tidak bisa akses config MikroTik
   → Perlu login admin dulu

2. ❌ Harga belum diset
   → Admin belum set harga untuk profile
   → Cek di admin panel

3. ❌ Saldo tidak cukup
   → Saldo agent kurang
   → Perlu topup

4. ❌ MikroTik tidak connect
   → IP/username/password salah
   → MikroTik offline

5. ❌ Function randNULC tidak ada
   → File include/config.php tidak di-load
   → Function helper tidak tersedia

SOLUSI CEPAT:
=============

LANGKAH 1: Enable Error Display
--------------------------------
File sudah diupdate dengan:
```php
error_reporting(E_ALL);
ini_set('display_errors', 1);
```

Upload generate.php yang baru dan test lagi.
Error akan muncul di halaman.

LANGKAH 2: Cek Prerequisites
-----------------------------
1. ✅ Admin sudah login MikhMon?
2. ✅ Session MikroTik aktif?
3. ✅ Harga sudah diset untuk profile?
4. ✅ Saldo agent mencukupi?
5. ✅ MikroTik bisa diakses?

LANGKAH 3: Test Manual
-----------------------
1. Login sebagai admin MikhMon
2. Generate voucher dari admin panel
3. Jika berhasil, berarti MikroTik OK
4. Jika gagal, berarti masalah di MikroTik

LANGKAH 4: Cek Error Message
-----------------------------
Setelah klik generate, lihat pesan error:

"Session MikroTik tidak ditemukan"
→ Agent panel tidak bisa akses config
→ SOLUSI: Buat file bridge config

"Harga untuk profile ini belum diset"
→ Admin belum set harga
→ SOLUSI: Set harga di admin panel

"Saldo tidak mencukupi"
→ Saldo kurang
→ SOLUSI: Topup saldo

"Gagal terhubung ke MikroTik"
→ MikroTik offline atau config salah
→ SOLUSI: Cek MikroTik

"Error parsing MikroTik config"
→ Format config salah
→ SOLUSI: Cek file config.php

SOLUSI ALTERNATIF:
==================

Karena agent panel tidak bisa akses config MikroTik
langsung (security), kita perlu buat API bridge.

OPSI 1: Gunakan Admin API
--------------------------
Agent panel call API admin panel untuk generate.

File: mikhmon/api/agent_generate.php
```php
<?php
session_start();
include('../include/config.php');
include('../include/db_config.php');
include('../lib/Agent.class.php');
include('../lib/routeros_api.class.php');

// Verify agent
$agentId = $_POST['agent_id'] ?? 0;
$profileName = $_POST['profile'] ?? '';
$quantity = $_POST['quantity'] ?? 1;

// Get session
$sessions = array_keys($data);
$session = null;
foreach ($sessions as $s) {
    if ($s != 'mikhmon') {
        $session = $s;
        break;
    }
}

// Connect & generate
// ... (logic generate)

echo json_encode(['success' => true, 'vouchers' => $vouchers]);
?>
```

OPSI 2: Simpan Config di Database
----------------------------------
Simpan MikroTik config di database agent_settings.

OPSI 3: Generate via Admin Panel
---------------------------------
Agent request generate, admin approve & generate.

REKOMENDASI SAYA:
=================

Untuk saat ini, cara termudah:

1. Buat API endpoint di admin panel
2. Agent panel call API tersebut
3. API generate voucher
4. Return hasil ke agent panel

Atau lebih simple:

1. Agent hanya bisa lihat harga & saldo
2. Generate voucher tetap via admin panel
3. Admin generate atas nama agent
4. Saldo agent terpotong otomatis

NEXT STEPS:
===========

Beri tahu saya error message yang muncul
setelah upload generate.php yang baru,
nanti saya buatkan solusi yang tepat.

===============================================
